/**
 * Script of the "video detail" page
 */
var PageVideo = new (function() {
	var $this = this;
	var node;
	
	/**
	 * Initialization
	 * If the object node don't exist, we cloned it for later
	 * Load the last video seen
	 */
	this.init = function(e, data) {
		if (!node) {
			node = $("#page-video #video-object").clone(true);
		}
		if (!$("#page-video #video-id").val()) {
			loadLastVideo();
		}
	}
	
	/**
	 * Load the last video seen
	 */
	function loadLastVideo() {
		if (window.localStorage['page_videoDetail']) {
			var o = JSON.parse(window.localStorage['page_videoDetail']);
			var videodata = o.video;
			var video = new VideoItem(videodata);
			video.fillPageVideoDetail();
		}
	}
	
	/**
	 * Save the last video
	 */
	this.setLastVideo = function(videodata) {
		window.localStorage['page_videoDetail'] = JSON.stringify({
			video: videodata
		});
	}
	
	/**
	 * When cloaing the page, we delete the node
	 */
	function onClose() {
		var elem = $("#page-video #video-object");
		elem.before(node.clone(true));
		elem.remove();
	}
	
	$("body").on("pagebeforeshow", "#page-video", this.init);
	$("body").on("pagebeforehide", "#page-video", onClose);

});